#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2024. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
"""
Time: 2024/08/17
Author:
Description: Restful APIs for feature
"""

from vulcanus.conf.constant import UserRoleType
from vulcanus.restful.resp import state
from vulcanus.restful.response import BaseResponse
from zeus.user_access_service.app import cache
from zeus.user_access_service.app.proxy.feature import FeatureProxy
from zeus.user_access_service.app.serialize.feature import UpdateFeatureSchema


class Features(BaseResponse):
    """
    Interface for features
    """

    @BaseResponse.handle(schema=UpdateFeatureSchema, proxy=FeatureProxy)
    def post(self, callback: FeatureProxy, **params):
        """
        update features status(on or off)

        Args:
            feature (List): feature id with its status. e.g [{feature_id:id_string,enable:true}]

        Returns:
            dict: response body
        """
        if cache.user_role != UserRoleType.ADMINISTRATOR:
            return self.response(code=state.PERMESSION_ERROR)
        update_res = callback.update_feature(params["feature"])
        if update_res != state.SUCCEED:
            return self.response(code=update_res, message="Failed to switch features status。")
        return self.response(code=state.SUCCEED)

    @BaseResponse.handle(proxy=FeatureProxy)
    def get(self, callback: FeatureProxy):
        """
        get features info

        Returns:
            dict: response body
        """
        status, features_info = callback.query_all_features()
        if status != state.SUCCEED:
            return self.response(code=status, message="Failed to switch features status。")
        return self.response(code=state.SUCCEED, data=features_info)


class QueryActiveFeatures(BaseResponse):
    """
    Interface for querying active features
    """

    @BaseResponse.handle(proxy=FeatureProxy)
    def get(self, callback: FeatureProxy):
        """
        get active features info

        Returns:
            dict: response body
        """
        status, features_info = callback.query_activated_features()
        if status != state.SUCCEED:
            return self.response(code=status, message="Failed to switch features status。")
        return self.response(code=state.SUCCEED, data=features_info)